package com.cuit.qx.bean;

import com.cuit.qx.utils.DynamicBean;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings({"serial" })
public class PageList<T> implements java.io.Serializable
{
	// 总记录数
	private long total;
	
	private List<T> rows;
	
	private List<T> data;
	
	public PageList()
	{
	}
	
	public long getTotal()
	{
		return total;
	}
	
	public void setTotal(long total)
	{
		this.total = total;
	}

	
	public List<T> getRows()
	{
		return rows;
	}


	public void setRows(List<T> rows)
	{
		this.rows = rows;
	}
	
	@SuppressWarnings("unchecked")
	public PageList(List<T> rows)
	{
		super();
		PageInfo<T> page = new PageInfo<T>(rows);
		this.total = page.getTotal();
		this.rows = (List<T>) formatDynamicBean(page.getList());
	}
	
	@SuppressWarnings("unchecked")
	public PageList(long total, List<T> data)
	{
		super();
		this.total = total;
		this.data = data;
		this.rows = (List<T>) formatDynamicBean(data);
	}
	
	public List<T> getData()
	{
		return data;
	}

	
	public void setData(List<T> data)
	{
		this.data = data;
	}
	
	/**
	 * 格式化方法
	 */
	private Object formatDynamicBean(Object data)
	{
		if (data instanceof List)
		{
			return handPageList(data);
		}
		return data;
	}
	
	/**
	 * 处理pageList
	 * 
	 * @param data
	 */
	private List<Object> handPageList(Object data)
	{
		List<Object> list = null;
		if (data != null)
		{
			list = new ArrayList<Object>();
			@SuppressWarnings("unchecked")
			List<Object> resultList = (List<Object>) data;
			for (int i = 0; i < resultList.size(); i++)
			{
				if (resultList.get(i) instanceof DynamicBean)
				{
					list.add(((DynamicBean)resultList.get(i)).getMap());
				}
				else
				{
					list.add(resultList.get(i));
				}
			}
		}
		return list;
	}
	
	
	
}
